package com.ease.actionscript.serialization;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import flex.messaging.io.BeanProxy;

/**
 * 
 * http://stackoverflow.com/questions/4611992/flex-lcds-serializing-inputstream- as-bytearray-using-beanproxy
 * 
 * @author nathanleewei
 * 
 */
public class InputStreamBeanProxy extends BeanProxy {

	/**
	 *
	 */
	private static final long serialVersionUID = 7975463746436116852L;

	public Object getValue(Object instance, String propertyName) {
		Object value = super.getValue(instance, propertyName);
		if (value instanceof InputStream) {
			value = getByteArray((InputStream) value);
		} else {
			value = super.getValue(instance, propertyName);
		}
		return value;
	}

	private byte[] getByteArray(InputStream stream) {
		try {
			byte[] bytes = IOUtils.toByteArray(stream);
			return bytes;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
